package cn.com.pxto.web.listener;

import java.util.HashMap;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.pxto.global.varables.LogonInfo;

public class LogedSessionListener implements HttpSessionBindingListener {
	Log log = LogFactory.getLog(LogedSessionListener.class); 
	private ServletContext application;
	
	public LogedSessionListener(ServletContext application) {
		if (application == null)
			throw new IllegalArgumentException("Null application is not accept.");
		this.application = application;
	}
//	��LogonInfo��ӵ����session map��
	public void valueBound(HttpSessionBindingEvent arg0) {
		HashMap activedSession = (HashMap)application.getAttribute("activedSession");
		if (activedSession==null) activedSession = new HashMap();
		//��LogonInfo��ӵ����session map��
		LogonInfo logonInfo = (LogonInfo)arg0.getSession().getAttribute("logonInfo");
		activedSession.put(arg0.getSession(),logonInfo);
		application.setAttribute("activedSession",activedSession);
	}
	
//	��LogonInfo�ӻ��session map��ɾ��
	public void valueUnbound(HttpSessionBindingEvent arg0) {
		HashMap activedSession = (HashMap)application.getAttribute("activedSession");
		if (activedSession!=null) activedSession.remove(arg0.getSession());
		application.setAttribute("activedSession",activedSession);
		if (log.isDebugEnabled()){
			log.debug("LogedSessionListener added one session");
		}
	}
}
